package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

/**
 * 图像覆盖修复
 * 
 * @author zincredible
 * @date 2019/07/18 10:09:48
 */
public class ImageCover extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton button;
	private BufferedImage image;

	public void init() {
		setSize(600, 800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		button = new JButton("保存图片");
		addSaveImageLisentner(button);
		add(button, BorderLayout.NORTH);
		generateImage();
		setVisible(true);

	}

	private void addSaveImageLisentner(JButton btn) {
		btn.addActionListener(e -> {
			JFileChooser fileChooser = new JFileChooser("D:\\");
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnVal = fileChooser.showOpenDialog(fileChooser);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				String filePath = fileChooser.getSelectedFile().getAbsolutePath();
				try {
					ImageIO.write(image, "jpg", new File(filePath + "\\" + System.currentTimeMillis() + ".jpg"));
					JOptionPane.showMessageDialog(this, "图片保存成功");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		});
	}

	private void generateImage() {
		try {
			image = ImageIO.read(new File("C:\\Users\\ZC20181120-1\\Desktop\\123.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		image.createGraphics();
		Graphics2D g = (Graphics2D) image.getGraphics();
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int imageHeigh = image.getHeight();
		int imageWight = image.getWidth();
		System.out.println(imageHeigh + "," + imageWight);

		for (int i = 0; i < imageHeigh; i++) {
			if (i >= 10 && i <= 110) {
				for (int j = 0; j < imageWight; j++) {
					if (j >= 10 && j <= 100) {
						image.setRGB(i, j, image.getRGB(getRandom(200, 300), getRandom(200, 300)));
					}
				}
			}

		}
		g.setColor(Color.BLACK);
		g.setFont(new Font("新宋体", Font.PLAIN, 18));
		g.drawString("证明-张思科", 10, 120);
		JLabel label = new JLabel();
		Icon icon = new ImageIcon(image);
		label.setIcon(icon);
		JScrollPane scroolPicLeft = new JScrollPane(label);
		add(scroolPicLeft, BorderLayout.CENTER);

	}

	/**
	 * 获取[x,y]区间随机数
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	private int getRandom(int x, int y) {
		int num = 0;
		// 说明：两个数在合法范围内，并不限制输入的数哪个更大一些
		if (x < 0 || y < 0) {
			return num;
		} else {
			int max = x > y ? x : y;
			int min = x < y ? x : y;
			int mid = max - min;// 求差
			// 产生随机数
			num = (int) (Math.random() * (mid + 1)) + min;
		}
		return num;
	}

	/**
	 * 获取某个位置像素点的RGB
	 * 
	 * @param image
	 * @param x
	 * @param y
	 * @return
	 */
	public int[] getRGB(BufferedImage image, int x, int y) {
		int[] rgb = new int[3];
		int pixel = image.getRGB(x, y); // 下面三行代码将一个数字转换为RGB数字
		rgb[0] = (pixel & 0xff0000) >> 16;
		rgb[1] = (pixel & 0xff00) >> 8;
		rgb[2] = (pixel & 0xff);
		return rgb;
	}

	public static byte hexToByte(String inHex) {
		return (byte) Integer.parseInt(inHex, 16);
	}

	public static byte[] hexToByteArray(String inHex) {
		int hexLen = inHex.length();
		byte[] result;
		if (hexLen % 2 == 1) {
			hexLen++;
			result = new byte[(hexLen / 2)];
			inHex = "0" + inHex;
		} else {
			result = new byte[(hexLen / 2)];
		}
		int j = 0;
		for (int i = 0; i < hexLen; i += 2) {
			result[j] = hexToByte(inHex.substring(i, i + 2));
			j++;
		}
		return result;
	}

	/**
	 * 将字节 转换为字符串
	 *
	 * @param src 需要转换的字节数组
	 * @return 返回转换完之后的数据
	 */
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {

			System.out.println(new String(src) + "," + (src[i] & 0xFF));

			String hv = Integer.toString(src[i]);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv + "-");
		}
		return stringBuilder.toString();
	}

	public static void main(String[] args) {
		new ImageCover().init();
	}
}
